package com.ysd.lis.service.inv.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.inv.InvSupplier;
import com.ysd.lis.entity.inv.InvSupplierAppraise;
import com.ysd.lis.mapper.inv.InvSupplierAppraiseMapper;
import com.ysd.lis.mapper.inv.InvSupplierMapper;
import com.ysd.lis.request.inv.InvSupplierDto;
import com.ysd.lis.request.inv.InvSupplierPageParam;
import com.ysd.lis.service.inv.InvSupplierService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-01-31
 */
@Service
public class InvSupplierServiceImpl extends ServiceImpl<InvSupplierMapper, InvSupplier> implements InvSupplierService {
    @Autowired
    InvSupplierMapper invSupplierMapper;
    @Autowired
    InvSupplierAppraiseMapper invSupplierAppraiseMapper;
    @Override
    public Result findInvSupplierPageList(InvSupplierPageParam pageParam) {
        Page<InvSupplier> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<InvSupplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvSupplier::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvSupplier::getSupplierName, pageParam.getSeachValue())
                    .or().like(InvSupplier::getSupplierCode, pageParam.getSeachValue())
                    .or().like(InvSupplier::getInputCode,pageParam.getSeachValue()));
        }
        if (StringUtils.isNotBlank(pageParam.getSupplierType())){
            lambdaQueryWrapper.eq(InvSupplier::getSupplierType,pageParam.getSupplierType());
        }
        if(ToolsUtils.isNotEmpty(pageParam.getIsStop())){
            lambdaQueryWrapper.eq(InvSupplier::getIsStop,pageParam.getIsStop());
        }
        lambdaQueryWrapper.orderByAsc(InvSupplier::getSeq);
        Page<InvSupplier> supplierPage = invSupplierMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result addInvSupplier(InvSupplier supplier) {
        int insert = invSupplierMapper.insert(supplier);
        if(insert>0){
            return Result.succ(1,"添加成功",supplier);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editInvSupplier(InvSupplier supplier) {
        int updateById = invSupplierMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result deleteInvSupplier(InvSupplier supplier) {
        int deleteById = invSupplierMapper.deleteById(supplier.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", "");
        }else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result batchDeleteInvSupplierList(InvSupplierDto dto) {
        List<InvSupplier> supplierList = dto.getSupplierList();
        List<String> ids = supplierList.stream().map(InvSupplier::getId).collect(Collectors.toList());
        //查询供应商下是否有评价登记
        LambdaQueryWrapper<InvSupplierAppraise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvSupplierAppraise::getDelFlag,0)
                .in(InvSupplierAppraise::getSupplierId,ids);
        Integer cnt = invSupplierAppraiseMapper.selectCount(queryWrapper);
        if(cnt>0){
            return Result.succ(1, "供应商下有登记评价，不能删除", null);
        }

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Override
    public Result findInvSupplierList(InvSupplierPageParam pageParam) {
        LambdaQueryWrapper<InvSupplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvSupplier::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvSupplier::getSupplierName, pageParam.getSeachValue())
                    .or().like(InvSupplier::getSupplierCode, pageParam.getSeachValue())
                    .or().like(InvSupplier::getInputCode,pageParam.getSeachValue()));
        }
        if (StringUtils.isNotBlank(pageParam.getSupplierType())){
            lambdaQueryWrapper.eq(InvSupplier::getSupplierType,pageParam.getSupplierType());
        }
        if(ToolsUtils.isNotEmpty(pageParam.getIsStop())){
            lambdaQueryWrapper.eq(InvSupplier::getIsStop,pageParam.getIsStop());
        }
        lambdaQueryWrapper.orderByAsc(InvSupplier::getSeq);
        List<InvSupplier> supplierPage = invSupplierMapper.selectList( lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }
}
